$(function(){
  //function gyro(a,b){
  /*
   * 0 a : 800%
   * 1 b : 832%
   * 2 c : 866%
   * 3 d : 900%
   *
   */
  var match = {
    0 : "800%",
    1 : "832%",
    2 : "866%",
    3 : "900%"
  };

  var flag = false;

  $("#trigger").on("click", function(){
    if (flag) {
      return;
    }
    flag = true;
    // get random count

    if ($.cookie('slot')) {
      $.fancybox.open('<p style="font-size:14px; line-height:100px; text-align:center">Sorry, 您已经抽奖过一次了</p>',{
        closeBtn: false,
        autoSize: false,
        width: 300,
        height: 100
      });
      flag = false;
      return false;
    }

    var n = [];
    if ($.inArray(parseInt(SLOT.number), SLOT.prize.cloth) !== -1) {
      n = [1, 1, 1, 1];
    } else if ($.inArray(parseInt(SLOT.number), SLOT.prize.cup) !== -1) {
      n = [2, 2, 2, 2];
    } else {
      do {
        n.push(Math.round(Math.random() * 3));
      } while(n.length < 4);
      if (n[0] == n[1] == n[2] == n[3]) {
        n[3] = Math.abs(n[3] - 1);
      }
    }

    e = {};
    e.a = match[n[0]];
    e.b = match[n[1]];
    e.c = match[n[2]];
    e.d = match[n[3]];
    $(this).css("background-position", "-844px 0");
    slot(e);

    //set cookie
    $.cookie('slot', '1', {expires: 30, path: '/'});
  });
  function slot(e){
    var duration = 3000;
    jQuery.each(e, function(k, v){
      duration += 200;
      $("#slot" + k).animate({
        "border-spacing" : v
      },
      {
        step: function(now, fx) {
          $(this).css("background-position", "0 " + now + "%");
        },
        duration: duration,
        complete: function(){
           $(this).css("background-position", "0 " +(parseInt(v) - 800 ) + "%");
           if (k == "d") {
             flag = false;
           }
        }
      });
    });
    //$("#two").animate({
    //"background-position-y": e.b
    //}, 3200, function(){
       //$(this).css("background-position-y", (parseInt(e.b) - 800 ) + "%");
    //});
    //$("#three").animate({
     //"background-position-y": e.c
    //}, 3400, function(){
       //$(this).css("background-position-y", (parseInt(e.c) - 800 ) + "%");
    //});
    //$("#four").animate({
     //"background-position-y": e.d
    //}, 3600, function(){
       //$(this).css("background-position-y", (parseInt(e.d) - 800 ) + "%");
    //});
  }

});

// A,B,C,D 各自对应的y位移百分比
// 根据算法 所返回 的那个字母或卡片，给他们相应的百分比

//ie6 png fix
//DD_belatedPNG.fix('.sina, .renren, .facebook, .twitter, .osllogo, .slotmachine, .letter, .trigger, .actititle, .flatloc');
$(function(){
  //sign in & sign up
  //$("#btsubmit").fancybox({
  //});
  $.fancybox.open('#login',{
    width: 300,
    minHeight: 360,
    autoHeight: true,
    autoSize: false,
    closeBtn: false,
    padding: 0
  });

  //tips
  $.fancybox.open('#usertips',{
    width: 390,
    height: 245,
    autoSize: false,
    closeBtn: false,
    padding: 0
  });
});
